package hn.cch.spring_boot_security_rest.aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ApiAspect {

    private static Logger logger = LoggerFactory.getLogger(ApiAspect.class);

    //本地线程变量
    ThreadLocal<Object> threadLocal = new ThreadLocal<>();


    @Pointcut("@annotation(hn.cch.spring_boot_security_rest.annotation.ApiAnnotation)")
    public void pointcut() {

    }
    @Before("pointcut()")
    public void before(JoinPoint joinPoint) throws Throwable {
        logger.info("before");
    }

    // @Around("pointcut()")
    // public ResponseEntity around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    //     logger.info("around before");
    //     Object object = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
    //     logger.info("around after");
    //     if (object==null){
    //         return new ResponseEntity<>(HttpStatus.OK);
    //     }
    //     if (!(object instanceof ResponseEntity)){
    //         return new ResponseEntity<>(object, HttpStatus.OK);
    //     }
    //     ;
    //     ResponseEntity responseEntity = (ResponseEntity) object;
    //     Response<Object> response = new Response<>();
    //     response.setData(responseEntity.getBody());
    //     response.setCode(String.valueOf(responseEntity.getStatusCodeValue()));
    //     response.setInfo(responseEntity.getStatusCode().getReasonPhrase());
    //     return new ResponseEntity<>(response, HttpStatus.OK);
    //
    // }

    @After("pointcut()")
    public void after(JoinPoint joinPoint) throws Throwable {
        logger.info("after");
    }
    @AfterThrowing(throwing ="throwable", pointcut = "pointcut()")
    public void afterThrowing(Throwable throwable) {
        logger.info("afterThrowing");
    }

    @AfterReturning(returning = "object", pointcut = "pointcut()")
    public void afterReturning(Object object) {
        logger.info("afterReturning");
    }


}
